//
//  BranchOpenOrganizationHeaderView.m
//  ShangHaiHuDongApp
//
//  Created by 刘应平 on 2017/6/15.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "BranchOpenOrganizationHeaderView.h"
#import "LPCostomNinetyLabelAndImageView.h"
@interface BranchOpenOrganizationHeaderView ()
/**  标题  */
@property (strong, nonatomic) UILabel  *headLine;
/**  数量  */
@property (strong, nonatomic) UILabel  *number;
/**  书记  */
@property (strong, nonatomic) UILabel  *shuji;
/**  委员  */
@property (strong, nonatomic) UILabel  *weiyuan;
/**  电话  */
@property (strong, nonatomic) UILabel  *phone;
/**  电话图片  */
@property (strong, nonatomic) UIImageView  *phoneImageView;
/**  地址图片  */
@property (strong, nonatomic) UIImageView  *addressImageView;
/**  地址  */
@property (strong, nonatomic) UILabel  *address;
/**  中心线  */
@property (strong, nonatomic) UIView  *centerView;
/**  空白视图  */
@property (strong, nonatomic) UIView  *backView;

@end
/**  350  */
@implementation BranchOpenOrganizationHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        [self addSubview:self.headLine];
//        [self addSubview:self.number];
//        [self addSubview:self.shuji];
//        [self addSubview:self.weiyuan];
        [self addSubview:self.phoneImageView];
        [self addSubview:self.phone];
        [self addSubview:self.addressImageView];
        [self addSubview:self.address];
        [self addSubview:self.centerView];
        [self addSubview:self.backView];
       
        
        self.headLine.sd_layout
        .leftSpaceToView(self, 15 * [UIScreen zj_screenWScale])
        .topSpaceToView(self, 15 * [UIScreen zj_screenHScale])
        .widthIs([UIScreen zj_screenWith] - 30 * [UIScreen zj_screenWScale])
        .heightIs(15 * [UIScreen zj_screenHScale]);
//        
//        self.number.sd_layout
//        .topSpaceToView(self.headLine, 20 * [UIScreen zj_screenHScale])
//        .leftEqualToView(self.headLine)
//        .heightIs(15 * [UIScreen zj_screenHScale])
//        .widthIs([UIScreen zj_screenWith] - 30 * [UIScreen zj_screenWScale]);
//        
//        self.shuji.sd_layout
//        .topSpaceToView(self.number, 15 * [UIScreen zj_screenHScale])
//        .leftEqualToView(self.headLine)
//        .heightIs(15 * [UIScreen zj_screenHScale])
//        .widthIs([UIScreen zj_screenWith] - 30 * [UIScreen zj_screenWScale]);
//        
//        self.weiyuan.sd_layout
//        .topSpaceToView(self.shuji, 15 * [UIScreen zj_screenHScale])
//        .leftEqualToView(self.headLine)
//        .heightIs(15 * [UIScreen zj_screenHScale])
//        .widthIs([UIScreen zj_screenWith] - 30 * [UIScreen zj_screenWScale]);
        
        self.phoneImageView.sd_layout
        .topSpaceToView(self.headLine, 10 * [UIScreen zj_screenHScale])
        .leftEqualToView(self.headLine)
        .heightIs(20 * [UIScreen zj_screenHScale])
        .widthIs(20 * [UIScreen zj_screenHScale]);
        
        self.phone.sd_layout
        .topEqualToView(self.phoneImageView)
        .leftSpaceToView(self.phoneImageView, 10 * [UIScreen zj_screenWScale])
        .heightIs(20 * [UIScreen zj_screenWScale])
        .widthIs([UIScreen zj_screenWith] - 80 * [UIScreen zj_screenWScale]);
        
        
        self.addressImageView.sd_layout
        .topSpaceToView(self.phoneImageView, 15 * [UIScreen zj_screenHScale])
        .leftEqualToView(self.headLine)
        .heightIs(20 * [UIScreen zj_screenHScale])
        .widthIs(20 * [UIScreen zj_screenHScale]);
        
        self.address.sd_layout
        .topEqualToView(self.addressImageView)
        .leftSpaceToView(self.addressImageView, 10 * [UIScreen zj_screenWScale])
        .heightIs(20 * [UIScreen zj_screenWScale])
        .widthIs([UIScreen zj_screenWith] - 80 * [UIScreen zj_screenWScale]);
        
        
        self.centerView.sd_layout.topSpaceToView(self.address, 20 * [UIScreen zj_screenHScale])
        .leftEqualToView(self)
        .rightEqualToView(self)
        .heightIs(1);
        
        self.backView.sd_layout.topSpaceToView(self.centerView, 10 * [UIScreen zj_screenHScale])
        .leftEqualToView(self)
        .rightEqualToView(self)
        .heightIs(90 * [UIScreen zj_screenHScale]);
        
        NSArray *array = @[@{@"image": @"dangzuzhi_dongtai", @"title": @"动态"},
                           @{@"image": @"dangzuzhi_ziyuan", @"title": @"资源"},
//                           @{@"image": @"dangzuzhi_xiangmu", @"title": @"项目"},
                           @{@"image": @"dangzuzhi_xuqiu", @"title": @"需求"},
                           @{@"image": @"dangzuzhi_huodong", @"title": @"活动"}];     CGFloat width = [UIScreen zj_screenWith] / array.count;
        for (int i = 0; i < array.count ; i ++) {
            LPCostomNinetyLabelAndImageView *nine = [[LPCostomNinetyLabelAndImageView alloc] initWithFrame:CGRectMake(width * i, 0, width, 90 * [UIScreen zj_screenHScale]) viewCount:array.count];
            nine.homeImageView.image = [UIImage imageNamed:array[i][@"image"]];
            nine.homeLabel.text = array[i][@"title"];
            nine.userInteractionEnabled = YES;
            nine.tag = i;
            [self.backView addSubview:nine];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
            [nine addGestureRecognizer:tap];
        }

        
        
        TenGrayShadowView *under = [TenGrayShadowView new];
        under.backgroundColor = [UIColor themeGrayColor];
        [self addSubview:under];
        under.sd_layout.leftEqualToView(self).bottomEqualToView(self).widthIs([UIScreen zj_screenWith]).heightIs(10 * [UIScreen zj_screenHScale]);
    }
    return self;
}

- (void)setModel:(BranchOpenOrganizationHeaderModel *)model{
    _model = model;
//    NSString *huajie = [NSString stringWithFormat:@"(%@)",@""];
//    NSString *strig= [NSString stringWithFormat:@"%@%@",model.DEPT_NAME,huajie];
//    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:strig];
//    [attr setAttributes:@{NSForegroundColorAttributeName: [UIColor themeColor], NSFontAttributeName : LYPFONT(14)} range:[strig rangeOfString:huajie]];
    
    self.headLine.text = model.DEPT_NAME;
    self.number.text = [NSString stringWithFormat:@"党员数: %@",model.PARTY_MEMBER];
    self.shuji.text = [NSString stringWithFormat:@"书记: %@",model.DEPTSECRETARY_NAME];
    self.weiyuan.text = [NSString stringWithFormat:@"委员: %@",model.DEPTCONTACTS_NAME];
    if (model.MOBILE.length == 0) {
        self.phone.text = @"暂无";
    }else{
        self.phone.text = model.MOBILE;
    }
    
    NSString *address;
    if (model.DEPTCONTACTS_ADDRESS.length == 0) {
        address = @"暂无";
    }else{
        address = model.DEPTCONTACTS_ADDRESS;
    }
    
    CGFloat height = [CalculateLabelHeight CalculateLabelHeightWithString:address font:16 * [UIScreen zj_textFontScale] width:[UIScreen zj_screenWith] - 80 * [UIScreen zj_screenWScale]].height;
    self.address.text = address;
    if ( height > 20 * [UIScreen zj_screenHScale]) {
        self.address.sd_layout.heightIs(height + 1);
        [self.address updateLayout];
    }
    
    
    self.headLine.attributedText = [UILabel setSpaceForLineWithText:model.DEPT_NAME lineSpace:5 * [UIScreen zj_screenHScale]];
    self.headLine.sd_layout.heightIs(model.HEIGHT);
    [self.headLine updateLayout];
   
}

- (void)tapClick:(UITapGestureRecognizer *)tap{
    if (self.delegate) {
        [self.delegate BranchOpenOrganizationHeaderTap_index:tap.view.tag];
    }
}

#pragma mark - getter
- (UILabel *)headLine{
    if (!_headLine) {
        _headLine = ({
            UILabel *label = [UILabel new];
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [UIColor textBlackColor];
            label.backgroundColor = [UIColor clearColor];
            label.font = LYPBOLDFONT(16);
            label.numberOfLines = 0;
            label;
        });
    }
    return _headLine;
}

- (UILabel *)number{
    if (!_number) {
        _number = ({
            UILabel *label = [UILabel new];
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [UIColor textGrayColor];
            label.backgroundColor = [UIColor clearColor];
            label.font = LYPFONT(16);
            label;
        });
    }
    return _number;
}


- (UILabel *)phone{
    if (!_phone) {
        _phone = ({
            UILabel *label = [UILabel new];
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [UIColor textGrayColor];
            label.backgroundColor = [UIColor clearColor];
            label.font = LYPFONT(16);
            label;
        });
    }
    return _phone;
}


- (UILabel *)weiyuan{
    if (!_weiyuan) {
        _weiyuan = ({
            UILabel *label = [UILabel new];
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [UIColor textGrayColor];
            label.backgroundColor = [UIColor clearColor];
            label.font = LYPFONT(16);
            label;
        });
    }
    return _weiyuan;
}


- (UILabel *)shuji{
    if (!_shuji) {
        _shuji = ({
            UILabel *label = [UILabel new];
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [UIColor textGrayColor];
            label.backgroundColor = [UIColor clearColor];
            label.font = LYPFONT(16);
            label;
        });
    }
    return _shuji;
}

- (UILabel *)address{
    if (!_address) {
        _address = ({
            UILabel *label = [UILabel new];
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [UIColor textGrayColor];
            label.backgroundColor = [UIColor clearColor];
            label.font = LYPFONT(16);
            label.numberOfLines = 0;
            label;
        });
    }
    return _address;
}

- (UIImageView *)addressImageView{
    if (_addressImageView == nil) {
        _addressImageView = [UIImageView new];
        _addressImageView.contentMode = UIViewContentModeCenter;
        _addressImageView.image = [UIImage imageNamed:@"icon_addressB"];
    }
    return _addressImageView;
}

- (UIImageView *)phoneImageView{
    if (_phoneImageView == nil) {
        _phoneImageView = [UIImageView new];
        _phoneImageView.contentMode = UIViewContentModeCenter;
        _phoneImageView.image = [UIImage imageNamed:@"icon_telphoneB"];
    }
    return _phoneImageView;
}

- (UIView *)centerView{
    if (!_centerView) {
        _centerView = [UIView new];
        _centerView.backgroundColor = [UIColor themeGrayColor];
    }
    return _centerView;
}
- (UIView *)backView{
    if (!_backView) {
        _backView = [UIView new];
    }
    return _backView;
}
@end
